package com.demo.service;

import cn.lingyangwl.framework.mybatis.mate.annotations.FieldUniqueCheck;
import cn.lingyangwl.framework.mybatis.mate.enums.OperationTypeEnum;
import com.demo.entity.SysUserPO;
import com.demo.mapper.UserMapper;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author shenguangyang
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    @Transactional
    public void a() {
        UserService currentProxy = (UserService) AopContext.currentProxy();
        SysUserPO sysUserPO = new SysUserPO();
        sysUserPO.setName("1");
        userMapper.insert(sysUserPO);
        // 操作数据库
        currentProxy.a();
        currentProxy.b();
        // throw new RuntimeException("xxx");
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void b() {
        SysUserPO sysUserPO = new SysUserPO();
        sysUserPO.setName("2");
        userMapper.insert(sysUserPO);
        throw new RuntimeException("xxx");
    }

    @FieldUniqueCheck(type = OperationTypeEnum.SAVE)
    @Override
    public void saveDemo(SysUserPO sysUserPO) {
        userMapper.insert(sysUserPO);
    }
}
